package com.linkgie.galaxyframework.runtime.service;

import com.linkgie.galaxyframework.security.ApplicationSecurityException;
import com.linkgie.galaxyframework.utils.RuntimeStatus;
import com.linkgie.galaxyframework.utils.service.ServiceContext;

public class ServiceRuntimeAssist {

	private static final ServiceRuntimeSPI SERVICE_RUNTIME_SPI;

	static {
		ServiceContext<ServiceRuntimeSPI> context = ServiceContext.load(ServiceRuntimeSPI.class);
		if (context.getSize() == 0 || context.getSize() > 1) {
			throw new ApplicationSecurityException(RuntimeStatus.ILLEGAL_RUNTIME_CONTEXT, "非法的运行时上下文");
		}
		SERVICE_RUNTIME_SPI = context.getServices().iterator().next();
	}

	/**
	 * 返回当前正直接执行的处于执行堆栈顶部的服务操作；
	 * 
	 * @return
	 */
	public static String getCurrentAction() {
		return SERVICE_RUNTIME_SPI.getServiceActionContext().getCurrentAction();
	}

	/**
	 * 是否正处于指定服务操作的执行堆栈范围；
	 * <p>
	 * 
	 * @param service 服务名称；
	 * @param action  操作名称；
	 * @return
	 */
	public static boolean isInAction(String service, String action) {
		return SERVICE_RUNTIME_SPI.getServiceActionContext().isInAction(service, action);
	}

}
